package com.dhnsoft.education.controller.article;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dhnsoft.education.entity.article.EduArticle;
import com.dhnsoft.education.entity.article.EduArticleCategory;
import com.dhnsoft.education.entity.article.vo.ArticleReq;
import com.dhnsoft.education.entity.edu.EduCourse;
import com.dhnsoft.education.entity.edu.EduTeacher;
import com.dhnsoft.education.entity.edu.vo.CourseQuery;
import com.dhnsoft.education.service.article.EduArticleCategoryService;
import com.dhnsoft.education.service.article.EduArticleService;
import com.dhnsoft.education.service.edu.EduTeacherService;
import com.dhnsoft.education.utils.R;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhousiren
 * @date 2022/1/6 22:26
 */
@RestController
@RequestMapping("/eduservice/article")
//@CrossOrigin
public class ArticleController {
    @Autowired
    private EduArticleService articleService;

    @Autowired
    private EduArticleCategoryService articleCategoryService;

    @Autowired
    private EduTeacherService teacherService;

//    @Autowired
//    private TOrderService orderService;

    @GetMapping("pageArticle/{current}/{limit}")
    public R pageArticle(@PathVariable long current,
                         @PathVariable long limit,
                         @RequestParam(required = false) String categoryId) {

        List<EduArticle> eduArticles = articleService.myQueryPage(current, limit, categoryId);

        Map<String, Object> map = new HashMap<>();
        map.put("totalCount",articleService.count(null));
        map.put("list",eduArticles);
        map.put("categoryList",articleCategoryService.list(null));

        return R.ok().data(map);
    }

    @GetMapping("getArticle/{id}")
    public R getArticle(HttpServletRequest request, @PathVariable String id){
        articleService.updatePageViewCount(id);
        //todo 判断是否收藏 点赞
        EduArticle article = articleService.getById(id);
//        boolean isBuy = orderService.isBuyCourse(JwtUtil.getMemberIdByJwtToken(request), BusinessEnum.ARTICLE.getType(), id);
        HashMap<String, Object> map = new HashMap<>(2);
//        map.put("isBuy",isBuy);
        map.put("article",article);
        return R.ok().data(map);
    }
    //后台分页查询
    @PostMapping("pageArticleCondition/{current}/{limit}")
    public R pageArticleCondition(@PathVariable long current, @PathVariable long limit, @RequestBody ArticleReq articleReq){
        Page<EduArticle> pageArticle = new Page<>(current, limit);
        //构建条件
        QueryWrapper<EduArticle> wrapper = new QueryWrapper<>();
        //多条件组合查询
        String title = articleReq.getTitle();
        String categoryId = articleReq.getCategoryId();
        if(!StringUtils.isEmpty(title)){
            wrapper.like("title",title);
        }
        if(!StringUtils.isEmpty(categoryId)){
            wrapper.eq("category_id",categoryId);
        }
        articleService.page(pageArticle,wrapper);
        long total = pageArticle.getTotal();//总记录数
        List<EduArticle> records = pageArticle.getRecords();//数据list集合数
        QueryWrapper<EduTeacher> teacherQueryWrapper = new QueryWrapper<>();
        QueryWrapper<EduArticleCategory> categoryQueryWrapper = new QueryWrapper<>();
        List<EduArticle> articleList = new ArrayList<>();
        //根据获取到集合中的教师id和文章类型id获取name
        for (int i = 0 ;i<records.size();i++) {
            String teacherId = records.get(i).getTeacherId();
            String categoryId2 = records.get(i).getCategoryId();
            teacherQueryWrapper.eq("id",teacherId);
            EduTeacher teacher = teacherService.getById(teacherId);
            categoryQueryWrapper.eq("id",categoryId2);
            EduArticleCategory category = articleCategoryService.getById(categoryId2);
            records.get(i).setTeacherName(teacher.getName());
            records.get(i).setCategoryName(category.getName());
            articleList.add(records.get(i));
        }
        return R.ok().data("total",total).data("rows",articleList);
    }
    //查询所有的文章分类
    @GetMapping("queryAllArticleCategory")
    public R queryAllArticleCategory(){
        List<EduArticleCategory> list = articleCategoryService.list(null);
        return R.ok().data("articleCategory",list);
    }
    //分页查询文章类型
    @GetMapping("pageArticleCategory/{current}/{limit}")
    public R pageArticleCategory(@PathVariable long current, @PathVariable long limit){
        Page<EduArticleCategory> categoryPage = new Page<>(current, limit);
        //构建条件
//        QueryWrapper<EduArticleCategory> wrapper = new QueryWrapper<>();
        articleCategoryService.page(categoryPage,null);
        long total = categoryPage.getTotal();//总记录数
        List<EduArticleCategory> records = categoryPage.getRecords();//数据list集合数
        return R.ok().data("total",total).data("rows",records);
    }
    //添加文章类型
    @PostMapping("addArticleCategory")
    public R addArticleCategory(@RequestBody EduArticleCategory articleCategory){
        articleCategoryService.save(articleCategory);
        return R.ok();
    }
    //根据文章类型Id查询文章类型信息
    @GetMapping("getCategoryById/{id}")
    public R getCategoryById(@PathVariable String id){
        QueryWrapper<EduArticleCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        EduArticleCategory one = articleCategoryService.getOne(queryWrapper);
        return R.ok().data("category",one);
    }
    //根据文章类型Id修改文章类型
    @PostMapping("updateArticleCategory")
    public R updateArticleCategory(@RequestBody EduArticleCategory articleCategory){
        articleCategoryService.updateById(articleCategory);
        return R.ok();
    }
    //根据文章类型Id删除文章
    @GetMapping("delArticleCategory/{id}")
    public R delArticleCategory(@PathVariable String id){
        QueryWrapper<EduArticleCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",id);
        articleCategoryService.remove(queryWrapper);
        return R.ok();
    }
    //添加文章
    @PostMapping("addArticle")
    public R addArticle(@RequestBody EduArticle eduArticle){
        eduArticle.setViewCount((long) 0);
        eduArticle.setMarkdown(true);
        articleService.save(eduArticle);
        return R.ok();
    }
    //根据文章Id查看文章信息
    @GetMapping("getArticleById/{id}")
    public R getArticleById(@PathVariable String id){
        QueryWrapper<EduArticle> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        EduArticle one = articleService.getOne(wrapper);
        return R.ok().data("articleInfo",one);
    }
    //根据文章Id修改文章信息
    @PostMapping("updateArticleById")
    public R updateArticleById(@RequestBody EduArticle article){
        articleService.updateById(article);
        return R.ok();
    }
    //根据文章Id删除文章
    @GetMapping("delArticleById/{id}")
    public R delArticleById(@PathVariable String id){
        QueryWrapper<EduArticle> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        articleService.remove(wrapper);
        return R.ok();
    }
}
